import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;


public class Exporter {
    public static void main(String[] args) {
        Properties params = Utilities.parseParams(args);
        String dataFilePath = params.getProperty("file");
        
        try {
        	long count = 0;
        	
        	Writer writer = new OutputStreamWriter(new FileOutputStream(dataFilePath), "UTF-8");
        	try {
	            DatabaseUtilities.initializeDatabase(
	        		params.getProperty("db"),
	            	params.getProperty("user"),
	            	params.getProperty("password"),
	            	false);
	            
	            PreparedStatement st = DatabaseUtilities.db.prepareStatement(
	            	"SELECT blogs.url, bloggers.gender, posts.url, posts.level, posts.date, posts.title, posts.body_text, posts.body_html, posts.id " +
	            	"FROM posts JOIN blogs ON posts.blog_id = blogs.id JOIN bloggers ON blogs.blogger_id = bloggers.id " +
	            	"ORDER BY posts.id");
	            ResultSet rs = st.executeQuery();
	            try {
	            	while (rs.next()) {
	            		exportPost(
            				writer,
	            			rs.getString(1), // blog url
	            			rs.getString(2), // blogger gender
	            			rs.getString(3), // post url
	            			rs.getString(4), // level
	            			rs.getLong(5),   // date
	            			rs.getString(6), // title
	            			rs.getString(7), // body text
	            			rs.getString(8), // body html
	            			rs.getLong(9)    // post id
	            		);
	            		
	                    count++;
	                    if (count % 10 == 0) {
	                    	System.out.println("Processed " + count + " posts");
	                    }
	            	}
	            } finally {
	            	rs.close();
	                st.close();
	            }
        	} finally {
        		writer.close();
        	}
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    private static void exportPost(
		Writer writer,
		String blogUrl,
		String bloggerGender,
		String postUrl,
		String level,
		long date,
		String title,
		String text,
		String html,
		long postId) throws IOException, SQLException {
    	char sep = '\t';
    	writer.write(blogUrl); writer.write(sep);
    	writer.write(bloggerGender); writer.write(sep);
    	writer.write(postUrl); writer.write(sep);
    	writer.write(level); writer.write(sep);
    	writer.write(formatDate(date)); writer.write(sep);
    	writer.write(title); writer.write(sep);
    	writer.write(escapeText(text)); writer.write(sep);
    	writer.write(escapeText(html)); writer.write(sep);
    	
        PreparedStatement st = DatabaseUtilities.db.prepareStatement(
            "SELECT tagged_text, paragraph_index, in_paragraph_index FROM sentences WHERE post_id = ? ORDER BY paragraph_index, in_paragraph_index");
        st.setLong(1, postId);
        
        ResultSet rs = st.executeQuery();
        try {
        	long lastParagraphId = -1;
            while (rs.next()) {
                long paragraphId = rs.getLong(2);
                if (paragraphId != lastParagraphId) {
                	if (lastParagraphId >= 0) {
                		writer.write("   ");
                	}
                	lastParagraphId = paragraphId;
                } else {
                	writer.write("  ");
                }
                
                String taggedText = rs.getString(1);
                if (taggedText == null) {
                	continue;
                }
                
                String[] taggedWords = taggedText.split(" ");
                for (int i = 0; i < taggedWords.length; i++) {
                	if (i > 0) {
                		writer.write(' ');
                	}
                	
                	String taggedWord = taggedWords[i];
                	int slash = taggedWord.indexOf('/');
                	writer.write(taggedWord.substring(slash + 1));
                	writer.write('/');
                	writer.write(taggedWord.substring(0, slash).toLowerCase());
                }
            }
        } finally {
        	rs.close();
            st.close();
        }
    	writer.write('\n');
    }
    
    private static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    
    private static String formatDate(long date) {
    	return dateFormat.format(new Date(date * 1000));
    }
    
    private static String escapeText(String text) {
    	return text.replace('\t', ' ').replaceAll(" {2,}", " ").replaceAll("[\\r\\n]", "  ");
    }
}
